package com.liustar.oplog.writer.consumer;

import com.liustar.mq.rocketmq.MessageListener;
import com.liustar.oplog.constant.OpLogConst;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.logging.Logger;

/**
 * 业务监听实现Demo
 *
 * @author Leo
 */

public class SimpleMqConsumer implements MessageListener {
    /**
     * 日志服务
     */
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    private SimpleMqExecutor simpleMqExecutor;

    @Override
    public boolean onMessage(Message message) {
        logger.info("监听到MQ消息:Topic[{" + message.getTopic() + "}]:Tags[{" + message.getTags() + "}]");

        //如果消息内容为空，直接返回True
        if (null == message.getBody()) {
            logger.warning("消息内容为空或 null");
            return true;
        }
        String tags = message.getTags();
        String topic = message.getTopic();

        if (topic.equals(OpLogConst.OPLOG_TOPIC)) {
            return simpleMqExecutor.doWriteOpLog(message, tags);
        } else {
            logger.warning("警告！警告！警告！接收到消息，但没有匹配到对应的消费者执行器。消息内容为：" + new String(message.getBody()));
        }
        return false;
    }

}

